﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;


/// <summary>
///
/// </summary>
namespace WebSite.Core
{
    public class Project_Session
    {
        private static string SESSION_USER_INFO_KEY = ConstHelper.SESSION_USER_INFO_KEY;

        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static LoginUser CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session[SESSION_USER_INFO_KEY] == null)
                {
                    // HttpContext.Current.Session.Clear();
                    return null;
                }
                return (HttpContext.Current.Session[SESSION_USER_INFO_KEY]) as LoginUser;
            }
            set
            {
                HttpContext.Current.Session[SESSION_USER_INFO_KEY] = value;
            }
        }

        public static int CurrentUserID
        {
            get
            {
                return CurrentUser == null ? 0 : CurrentUser.UserId;
            }
        }

        public static int NewMessageCount
        {
            get
            {
                if (HttpContext.Current.Session[ConstHelper.SESSION_USER_NEW_MSG_COUNT_KEY] == null)
                {
                    return 0;
                }
                return (int)HttpContext.Current.Session[ConstHelper.SESSION_USER_NEW_MSG_COUNT_KEY];
            }
            set
            {
                HttpContext.Current.Session[ConstHelper.SESSION_USER_NEW_MSG_COUNT_KEY] = value;
            }
        }

        public static Maticsoft.Model.CarrefourCover.Baby CurrentUserBaby
        {
            get
            {
                return (HttpContext.Current.Session[ConstHelper.SESSION_USER_BABY_INFO_KEY]) as Maticsoft.Model.CarrefourCover.Baby;
            }
            set
            {
                HttpContext.Current.Session[ConstHelper.SESSION_USER_BABY_INFO_KEY] = value;
            }
        }

        public static void SetSession( int userid, string username )
        {
            Project_Session.CurrentUser = new LoginUser()
            {
                UserId = userid,
                UserName = username
            };

            RefreshBabyInfo();
            RefershNewMsgCount();
        }

        public static Maticsoft.Model.CarrefourCover.Baby RefreshBabyInfo()
        {
            if (CurrentUser == null)
            {
                return null;
            }

            Maticsoft.BLL.CarrefourCover.Baby bll = new Maticsoft.BLL.CarrefourCover.Baby();
            Project_Session.CurrentUserBaby = bll.GetModel( CurrentUserID );
            return Project_Session.CurrentUserBaby;
        }

        public static int RefershNewMsgCount()
        {
            Maticsoft.BLL.CarrefourCover.ShortMessage bll = new Maticsoft.BLL.CarrefourCover.ShortMessage();
            Project_Session.NewMessageCount = bll.getNewCount( CurrentUserID );
            return Project_Session.NewMessageCount;
        }

        public static void ClearSession()
        {
            HttpContext.Current.Session.Remove(SESSION_USER_INFO_KEY);
            // HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();

        }
    }

    /// <summary>
    /// 登录信息
    /// </summary>
    [Serializable]
    public class LoginUser
    {
        public int UserId { get; set; }

        public string UserName { get; set; }


    }

}
